package com.ll.xiaonew.commons.core.threadpool;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author new
 * @date 2023/3/30 11:26
 * @Description: 项目中创建线程使用线程池
 */
@Component
public class ProjectThreadPool {
    private Integer code = Runtime.getRuntime().availableProcessors();

    private Integer max = code*2;

    private Integer time = 3000;

    private Integer length = 500;

    @Bean
    public ThreadPoolExecutor poolExecutor(){
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                code,
                max,
                time,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(length),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        return threadPoolExecutor;
    }

}
